﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class AnnouncementBll : IAnnouncementBll
    {
        private IAnnouncementDal _ann;
        private IMapper _mapper;
        public AnnouncementBll(IAnnouncementDal ann, IMapper mapper)
        {
            _ann = ann;
            _mapper = mapper;
        }

        public async Task<int> AddAnnouncementAsync(int announcementType, string noticeInfo, DateTime EndTime)
        {
            return await _ann.AddAsync(new Announcement 
            {
                AnnouncementType = announcementType,
                NoticeInfo = noticeInfo,
                EndTime = EndTime
            });
        }

        public async Task<int> DeleteAnnouncementAsync(int id)
        {
            var data = await _ann.QueryAsync(id);
            return data == null ? -1 : await _ann.DeleteAsync(data);
        }

        public async Task<int> EditAnnouncementAsync(int id, int announcementType, string noticeInfo, DateTime EndTime)
        {
            var data = await _ann.QueryAsync(id);
            if (data == null)
                return -1;
            data.AnnouncementType = announcementType;
            data.NoticeInfo = noticeInfo;
            data.EndTime = EndTime;
            return await _ann.EditAsync(data);
        }

        public async Task<List<AnnouncementDto>> GetAnnouncementAllAsync()
        {
            var data = await _ann.QueryAsync();
            return _mapper.Map<List<Announcement>,List<AnnouncementDto>>(data);
        }

        public async Task<AnnouncementDto> QueryAnnouncementByIdAsync(int id)
        {
            var data = await _ann.QueryAsync(id);
            return _mapper.Map<Announcement, AnnouncementDto>(data);
        }
    }
}
